package com.liuxinlong.modules.patent.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.PatentDepartment;
import com.liuxinlong.modules.entity.PatentDepartmentSpecial;
import com.liuxinlong.modules.patent.controller.request.ImportPatentDepartmentReq;
import com.liuxinlong.modules.patent.controller.request.QueryPatentDepartmentReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentDepartmentReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentSpecialReq;
import com.liuxinlong.modules.patent.service.PatentDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专利部门信息控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-09-01
 */
@RestController
@RequestMapping("/patent-department")
public class PatentDepartmentController {

    @Autowired
    private PatentDepartmentService patentDepartmentService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询专利部门信息
     *
     * @param req 请求信息
     * @return 专利部门信息列表
     */
    @PostMapping("page")
    @LogAnnotation(value ="分页查询",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageDepartmentInfo(@RequestBody QueryPatentDepartmentReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("name", req.getName());
        queryMap.put("sn", req.getSn());
        List<Map<String, Object>> dataList = patentDepartmentService.pageDepartmentInfo(queryMap);
        int total = patentDepartmentService.countDepartment(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询专利部门信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 新增专利部门信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value ="新增专利部门信息",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addDepartmentInfo(@RequestBody UpdatePatentDepartmentReq req) {
        PatentDepartment patentDepartment = new PatentDepartment();
        patentDepartment.setName(req.getName());
        patentDepartment.setSn(req.getSn());
        patentDepartment.setManage(req.getManage());
        patentDepartment.setStatus(req.getStatus());
        patentDepartment.setDutyUser(req.getDutyUser());
        patentDepartment.setBranch(req.getBranch());
        patentDepartment.setRemark(req.getRemark());
        patentDepartmentService.addDepartmentInfo(patentDepartment);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增专利部门信息成功");
        return resp;
    }

    /**
     * 修改专利部门信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value ="修改专利部门信息",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateDepartmentInfo(@RequestBody UpdatePatentDepartmentReq req) {
        PatentDepartment patentDepartment = new PatentDepartment();
        patentDepartment.setId(req.getId());
        patentDepartment.setName(req.getName());
        patentDepartment.setSn(req.getSn());
        patentDepartment.setManage(req.getManage());
        patentDepartment.setStatus(req.getStatus());
        patentDepartment.setDutyUser(req.getDutyUser());
        patentDepartment.setBranch(req.getBranch());
        patentDepartment.setRemark(req.getRemark());
        patentDepartmentService.updateDepartmentInfo(patentDepartment);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改专利部门信息成功");
        return resp;
    }

    /**
     * 删除专利部门信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value ="删除专利部门信息",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteDepartmentInfo(@RequestBody BaseDeleteReq req) {
        patentDepartmentService.deleteDepartmentInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除专利部门信息成功");
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value ="导入专利部门数据",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importPatentDepartments(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = patentDepartmentService.importDepartments((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入专利部门信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value ="导入专利部门数据",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddPatentDepartment(@RequestBody ImportPatentDepartmentReq req) {
        return patentDepartmentService.batchAddDepartment(req.getDataList());
    }

    /**
     * 导出专利部门信息
     *
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value ="导出专利部门信息",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportPatentDepartment(HttpServletResponse response) {
        patentDepartmentService.exportPatentDepartment(response);
    }

    /**
     * 分页查询专利部门特殊用户信息
     *
     * @param req 请求信息
     * @return 专利部门特殊用户信息列表
     */
    @PostMapping("special-page")
    public BaseResp pageDepartmentSpecialInfo(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = patentDepartmentService.pageDepartmentSpecialInfo(queryMap);
        int total = patentDepartmentService.countDepartmentSpecial(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询专利部门特殊用户信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 新增专利部门特殊用户信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("special-add")
    @LogAnnotation(value ="新增专利部门特殊用户信息",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addDepartmentSpecialInfo(@RequestBody UpdatePatentSpecialReq req) {
        PatentDepartmentSpecial patentDepartmentSpecial = new PatentDepartmentSpecial();
        patentDepartmentSpecial.setSpecialUser(req.getSpecialUser());
        patentDepartmentSpecial.setBranch(req.getBranch());
        patentDepartmentSpecial.setRemark(req.getRemark());
        patentDepartmentService.addDepartmentSpecialInfo(patentDepartmentSpecial);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增专利部门特殊用户信息成功");
        return resp;
    }

    /**
     * 修改专利部门特殊用户信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("special-update")
    @LogAnnotation(value ="修改专利部门特殊用户信息",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateDepartmentSpecialInfo(@RequestBody UpdatePatentSpecialReq req) {
        PatentDepartmentSpecial patentDepartmentSpecial = new PatentDepartmentSpecial();
        patentDepartmentSpecial.setId(req.getId());
        patentDepartmentSpecial.setSpecialUser(req.getSpecialUser());
        patentDepartmentSpecial.setBranch(req.getBranch());
        patentDepartmentSpecial.setRemark(req.getRemark());
        patentDepartmentService.updateDepartmentSpecialInfo(patentDepartmentSpecial);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改专利部门特殊用户信息成功");
        return resp;
    }

    /**
     * 删除专利部门特殊用户信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("special-delete")
    @LogAnnotation(value ="删除专利部门特殊用户信息",business = LogBusinessConstants.PATENT_DEPARTMENT,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteDepartmentSpecialInfo(@RequestBody BaseDeleteReq req) {
        patentDepartmentService.deleteDepartmentSpecialInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除专利部门特殊用户信息成功");
        return resp;
    }
}
